/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui.customize.ui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicScrollBarUI;

import lineage.ui.util.L1LoadImage;

/**
 * 自定义滚动条外观.
 * 
 * @version 2014年2月28日下午9:52:21
 * @author jrwz
 */
public class L1BasicScrollBarUI extends BasicScrollBarUI {
    /** 滚动条的宽度. */
    protected int width = 13;
    /** 滚动条的高度. */
    protected int height = 13;
    /** 图片路径：垂直滑块. */
    protected String verticalSliderURL = "./image/ScrollBarUI/verticalSlider.png";
    /** 图片路径：水平滑块. */
    protected String horizontalSliderURL = "./image/ScrollBarUI/horizontalSlider.png";
    /** 图片路径：垂直滑块背景. */
    protected String verticalBackgroundURL = "./image/ScrollBarUI/verticalBackground.png";
    /** 图片路径：水平滑块背景. */
    protected String horizontalBackgroundURL = "./image/ScrollBarUI/horizontalBackground.png";
    /** 图片路径：上按钮. */
    protected String upButtonURL = "./image/ScrollBarUI/upButton.png";
    /** 图片路径：下按钮. */
    protected String downButtonURL = "./image/ScrollBarUI/downButton.png";
    /** 图片路径：左按钮. */
    protected String leftButtonURL = "./image/ScrollBarUI/leftButton.png";
    /** 图片路径：右按钮. */
    protected String rightButtonURL = "./image/ScrollBarUI/rightButton.png";

    // 滚动条的首选大小
    @Override
    public Dimension getPreferredSize(JComponent c) {
        return new Dimension(width, height);
    }

    // 重绘滚动条的滑块
    @Override
    public void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        super.paintThumb(g, c, thumbBounds);
        int tx = thumbBounds.x;
        int ty = thumbBounds.y;
        int tw = thumbBounds.width;
        int th = thumbBounds.height;

        g.translate(tx, ty);

        Graphics2D g2 = (Graphics2D) g;
        Image img = null;
        switch (scrollbar.getOrientation()) {
        case JScrollBar.VERTICAL: // 具有垂直定向性
            img = L1LoadImage.getInstance().getImg(verticalSliderURL);
            break;
        case JScrollBar.HORIZONTAL: // 具有水平定向性
            img = L1LoadImage.getInstance().getImg(horizontalSliderURL);
            break;
        default:
            break;
        }
        g2.fillRect(0, 0, tw, th);
        g2.drawRect(0, 0, tw - 1, th - 1);
        g2.drawImage(img, 0, 0, tw, th, null);
    }

    // 重绘滑块的滑动区域背景
    @Override
    public void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
        int tx = trackBounds.x;
        int ty = trackBounds.y;
        int tw = trackBounds.width;
        int th = trackBounds.height;

        Graphics2D g2 = (Graphics2D) g;
        Image img = null;
        switch (scrollbar.getOrientation()) {
        case JScrollBar.VERTICAL: // 具有垂直定向性
            img = L1LoadImage.getInstance().getImg(verticalBackgroundURL);
            break;
        case JScrollBar.HORIZONTAL: // 具有水平定向性
            img = L1LoadImage.getInstance().getImg(horizontalBackgroundURL);
            break;
        default:
            break;
        }
        g2.fillRect(0, 0, tw, th);
        g2.drawRect(0, 0, tw - 1, th - 1);
        g2.drawImage(img, tx, ty, tw, th, null);
    }

    // 按钮(上,左)
    @Override
    protected JButton createIncreaseButton(int orientation) {
        return new BasicArrowButton(orientation) {
            private static final long serialVersionUID = 7187301146929017904L;

            @Override
            public void paintTriangle(Graphics g, int x, int y, int size, int direction, boolean isEnabled) {
                int w = getWidth();
                int h = getHeight();

                Graphics2D g2 = (Graphics2D) g;
                Image img = null;
                switch (this.getDirection()) {
                case BasicArrowButton.SOUTH:
                    img = L1LoadImage.getInstance().getImg(downButtonURL);
                    break;
                case BasicArrowButton.EAST:
                    img = L1LoadImage.getInstance().getImg(rightButtonURL);
                    break;
                }
                g2.fillRect(0, 0, w, h);
                g2.drawRect(0, 0, w - 1, h - 1);
                g2.drawImage(img, 0, 0, w, h, null);
            }
        };
    }

    // 按钮(下,右)
    @Override
    protected JButton createDecreaseButton(int orientation) {
        return new BasicArrowButton(orientation) {
            private static final long serialVersionUID = -6346873120342584948L;

            @Override
            public void paintTriangle(Graphics g, int x, int y, int size, int direction, boolean isEnabled) {
                int w = getWidth();
                int h = getHeight();

                Graphics2D g2 = (Graphics2D) g;
                Image img = null;
                switch (this.getDirection()) {
                case BasicArrowButton.NORTH:
                    img = L1LoadImage.getInstance().getImg(upButtonURL);
                    break;
                case BasicArrowButton.WEST:
                    img = L1LoadImage.getInstance().getImg(leftButtonURL);
                    break;
                }
                g2.fillRect(0, 0, w, h);
                g2.drawRect(0, 0, w - 1, h - 1);
                g2.drawImage(img, 0, 0, w, h, null);
            }
        };
    }
}
